<?php

namespace app\common\model;

class Term extends \think\Model
{
    protected $visible = ['id', 'name', 'start_date'];

    /**
     * 获取当前的默认学期信息
     *
     * @return void
     */
    public static function getDefaultTerm(string $date = null)
    {
        $id = model('SystemConfig')->getValueInt('CurTerm', 0);
        if ($id == 0 && $date != null) {
            return Term::autoFindTerm($date);
        }
        return Term::get($id);
    }

    /**
     * 根据日期,自动寻找合适的学期
     *
     * @return object|null 学期或null
     */
    public static function autoFindTerm(string $date)
    {
        //
        return Term::whereRaw(':date1 is not null and :date2 between start_date and date_add(start_date,INTERVAL 5 MONTH)', [
            'date1' => $date,
            'date2' => $date
        ])->find();
    }
}
